package com.mechat

import android.app.Application
import android.content.Context
import android.util.Log
import com.custom.base.config.BaseConfig
import io.rong.imkit.IMCenter
import io.rong.imkit.SpName
import io.rong.imlib.RongCoreClient
import io.rong.imlib.model.InitOption
import io.rong.imlib.model.InitOption.AreaCode
import io.rong.push.RongPushClient
import io.rong.push.pushconfig.PushConfig
import org.json.JSONObject


/**
 * 作者：
 * 日期：2022/3/1
 * 说明：
 */
class App : Application() {
    private var isDebug = false
    private var source = ""
    fun getApplicationContexts(): Context {
        return this
    }


    override fun onCreate() {
        super.onCreate()
//        AutoLayoutConifg.getInstance().useDeviceSize()
        initTextAndFormal(resources.getString(R.string.type))

        val appKey =if (isDebug) getString(R.string.rongyun_dev_key) else  getString(R.string.rongyun_online_key)
        val areaCode = AreaCode.NA

        val config: PushConfig = PushConfig.Builder()
            .enableFCM(true)
            .build()
        RongPushClient.setPushConfig(config)
        val initOption = InitOption.Builder()
            .setAreaCode(areaCode)
            .enablePush(true)
            .setNaviServer("nav.us-light-edge.com")
            .build()
        RongCoreClient.setServerInfo("nav.us-light-edge.com",null)
        IMCenter.init(this, appKey, initOption)
        BaseConfig.getInstance.init(
            this,
            debug = isDebug,
            printLog = isDebug,
            debugUrl = "http://112.124.66.71:9105",
            url = "https://sc-api2.mechat.club",
            successCode = "0",
            successCodeName = "code",
            successMsgName = "msg",
            pageName = "pageNum",
            pageSizeName = "pageSize",
            isHttpFailToast = true,
            titleBold = true,
            URLDecoder = false
        )

        BaseConfig.getInstance.setString(SpName.source,source)
        val jsonObject = JSONObject()
        jsonObject.put("channel","l1")
        jsonObject.put("appCode","sparksy")
        jsonObject.put("appVersion",BuildConfig.VERSION_CODE)
        jsonObject.put("token",BaseConfig.getInstance.getString(SpName.token,""))
        BaseConfig.getInstance.addHead("basicParams", jsonObject.toString())
    }

    /**
     * 根据bundle初始化
     * @param string
     */
    private fun initTextAndFormal(string: String) {
        Log.e("initTextAndFormal",string)
        isDebug = string == "yyb"
        source = string
    }


}